package select;
import java.util.*;
import java.io.*;
public class Select{

	
	
	
	static void Xs (Student a) {
		System.out.println("学生姓名:"+a.name+" 学生年龄:"+a.age+" 学生性别:"+a.sex+" 学生学号:"+a.number);
	}
	static void Tt (Teacher a) {
		System.out.println("教师姓名:"+a.name+" 教师年龄:"+a.age+" 教师性别:"+a.sex+" 教师编号:"+a.number);
		
	}
	static void Rtname (Class a) {
		System.out.println(a.mingcheng);
	}
	
	 
	
public static void main(String[]args) throws NotSerializableException,EOFException,ClassNotFoundException, IOException{
	try(Scanner sc = new Scanner(System.in)){
    int xueshengrenshu;
    xueshengrenshu = 0;
    int laoshirenshu;
    laoshirenshu = 0;
    int kc=0;
    
    int laoshi1 = -1;
	 int shijian1  = 0;
	 int  didian1 =0;
	 String mingcheng1 ="无";
	 Class[] cla = new Class[10] ;{
	    for (int i=0;i<10;i++) {
	    	Class temp = new Class(laoshi1,shijian1,didian1,mingcheng1);
	    	cla[i]= temp;
	    }
	    }
	
	 String name1 = "无";
	 String sex1 = "无";
	 int  xuehao1 =0;
	 int age1 =0;
	Student[] stu = new Student[30] ;{
   for (int i=0;i<30;i++) {
   	Student temp = new Student(name1,xuehao1,sex1,age1);
   	stu[i]= temp;
   }
   }
	
	 String tname1 = "无";
	 String tsex1  = "无";
	 int  trenshu1 =0;
	 int tage1 =0;
	Teacher[] tea = new Teacher[5] ;{
   for (int i=0;i<5;i++) {
   	Teacher temp = new Teacher(tname1,trenshu1,tsex1,tage1);
   	tea[i]= temp;
   }
   }
	/*
	 * ObjectInputStream stuin =new ObjectInputStream(new FileInputStream(stuf));
		Object obj=String.valueOf(stuin.toString());
		String temp1 = obj.toString();
		int ch1=-1,te1=0,te2=0,list=0;
		for(int i=0;i<temp1.length();i++) {
			String temp2= temp1.substring(i,1);
	 */

    
    	File stuf = new File("/Users/swk/Desktop/test/xuanke/student.txt"); 
      	File teaf = new File("/Users/swk/Desktop/test/xuanke/teacher.txt");
    	File claf = new File("/Users/swk/Desktop/test/xuanke/course.txt");
/*File read = new File("/Users/swk/Desktop/test/xuanke/student.txt"); 
		
		BufferedInputStream br = new BufferedInputStream(
				new FileInputStream(read));
		byte[] a = new byte[1024];
		int i = br.read(a) ;
		System.out.println(i+ new String(a));
    	
 */   	 
            if(stuf.exists()==false) {stuf.createNewFile();}
            if(teaf.exists()==false) {teaf.createNewFile();}
            if(claf.exists()==false) {claf.createNewFile();}
        ObjectInputStream in = new ObjectInputStream(new FileInputStream(stuf));
        stu = (Student[])in.readObject() ;
        in.close();
        ObjectInputStream in1 = new ObjectInputStream(new FileInputStream(teaf));
        tea = (Teacher[])in1.readObject() ;
        in1.close();
        ObjectInputStream in2 = new ObjectInputStream(new FileInputStream(claf));
        cla = (Class[])in2.readObject() ;
        in2.close();
        
        for (int i=0;i<30;i++) {
        	if(stu[i].number!=0) {
        		xueshengrenshu+=1;
        		}
        }
        for (int i=0;i<5;i++) {
        	if(tea[i].number!=0) {
        		laoshirenshu+=1;
        	}
        }
        for (int i=0;i<10;i++) {
        	if(cla[i].laoshi!=-1) {
        		kc+=1;
        	}
        }
        

    	/*FileInputStream s=new FileInputStream(stuf);

		String temp1 = s.toString();
		int ch1=-1,te1=0,te2=0,list=0;
		for(int i=0;i<temp1.length();i++) {
			String temp2= temp1.substring(i,i+1);
			if(temp2.equals(" ")) {
				ch1+=1;ch1=ch1%8;
				te2=i-te1;

				if(ch1==0) {
					String tmp=temp1.substring(te1, te2);
					stu[list].name=tmp;
					
					te1=i+1;		
				}else if(ch1==1) {
					int tmp=Integer.parseInt(temp1.substring(te1, te2));
					stu[list].number=tmp;
					te1=i+1;	
				}else if(ch1==2) {
					String tmp=temp1.substring(te1, te2);
					stu[list].sex=tmp;
					te1=i+1;
				}else if(ch1==3) {
					int tmp=Integer.parseInt(temp1.substring(te1, te2));
					stu[list].age=tmp;
					te1=i+1;
				}else if(ch1>3) {
					int tmp=Integer.parseInt(temp1.substring(te1, te2));
					stu[list].c[ch1-4]=tmp;
					te1=i+1;
				}
				}
			if(temp2.equals("\n")) {
				int tmp=Integer.parseInt(temp1.substring(te1, te2));
				stu[list].c[4]=tmp;
				te1=i+1;
				
				list=list+1;
			}	
		}
		Xs(stu[0]);
		xueshengrenshu=list;
		s.close();
	
    /*
     File claf = new File("/Users/swk/Desktop/test/xuanke/course.txt");
    	FileInputStream s=new FileInputStream(claf);

		String temp1 = s.toString();
     */
    
/*
    	FileInputStream s1=new FileInputStream(teaf);

		String temp11 = s.toString();
		int ch11=-1,te11=0,te21=0,list1=0;
		for(int i=0;i<temp11.length();i++) {
			String temp21= temp11.substring(i,i+1);
			if(temp21.equals(" ")) {
				ch11+=1;ch11=ch11%5;
				te21=i-te11;
				if(ch1==0) {
					String tmp=temp11.substring(te11, te21);
					tea[list1].name=tmp;
					te11=i+1;		
				}else if(ch11==1) {
					int tmp=Integer.parseInt(temp1.substring(te11, te21));
					tea[list1].number=tmp;
					te11=i+1;	
				}else if(ch11==2) {
					String tmp=temp11.substring(te11, te21);
					tea[list1].sex=tmp;
					te11=i+1;
				}else if(ch11==3) {
					int tmp=Integer.parseInt(temp11.substring(te11, te21));
					tea[list1].age=tmp;
					te11=i+1;
				}else if(ch11==4) {
					int tmp=Integer.parseInt(temp11.substring(te11, te21));
					tea[list1].ct[ch11-4]=tmp;
					te11=i+1;
				}
				}
			if(temp21.equals("\n")) {
				int tmp=Integer.parseInt(temp11.substring(te11, te21));
				tea[list1].ct[1]=tmp;
				te11=i+1;
				list1=list1+1;
			}	
		}
		laoshirenshu=list1;
		s1.close();
	
    
  

    	FileInputStream s2=new FileInputStream(claf);

		String temp12 = s.toString();
		int ch12=-1,te12=0,te22=0,list2=0;
		for(int i=0;i<temp12.length();i++) {
			String temp22= temp12.substring(i,i+1);
			if(temp22.equals(" ")) {
				ch12+=1;ch12=ch12%3;
				te22=i-te12;
				if(ch12==0) {
					int tmp=Integer.parseInt(temp12.substring(te12, te22));
					cla[list2].didian=tmp;
					te12=i+1;		
				}else if(ch12==1) {
					int tmp=Integer.parseInt(temp12.substring(te12, te22));
					cla[list2].shijian=tmp;
					te12=i+1;	
				}else if(ch12==2) {
					int tmp=Integer.parseInt(temp12.substring(te12, te22));
					cla[list2].didian=tmp;
					te12=i+1;
				}
				}
			if(temp22.equals("\n")) {
				String tmp=temp12.substring(te12, te22);
				cla[list2].mingcheng=tmp;
				te12=i+1;
				list2=list2+1;
			}	
		}
		laoshirenshu=list2;
		s2.close();
*/
 

    boolean d=true;
	while (d) {
	int check ;
	System.out.println("----------------------------------------");
	System.out.println("欢迎来到选课系统");
    System.out.println("学生输入1，老师输入2,退出输入0");
    check = sc.nextInt();
    if (check == 1) {
    	System.out.println("----------------------------------------");
    	 System.out.println("新建学生输入0，现有学生输入学号");
    	    check = sc.nextInt();
    	    Boolean x;
     	    x=true;
     	    if (xueshengrenshu<check) {x=false;System.out.println("输入错误");}
    	    if (check ==0) {
    	    	xueshengrenshu ++;
    	        System.out.println("输入姓名");
    	        String name = sc.next();
    	        System.out.println("输入性别");
    	        String sex = sc.next();
    	        System.out.println("输入年龄");
    	        int age = sc.nextInt();
    	        Student temp = new Student(name,xueshengrenshu,sex,age);
                stu[xueshengrenshu-1] = temp;   
                check = xueshengrenshu ;             
    	    }

       	        while (x) {
       	        int celect;
       	        System.out.println("----------------------------------------");
       	        
                Xs(stu[check-1]) ;
                int temp =0;
                
                System.out.println("----------------------------------------");
                 for (int i=0;i<5;i++) {
                	 if (stu[check-1].c[i]==-1) {
                		 temp ++;
                	 }
                	 else {
                		 System.out.println((i+1)+".");
                		 System.out.println("名称：");
        				 Rtname(cla[stu[check-1].c[i]]);//输出选择课表
        				 System.out.println("编号："+stu[check-1].c[i]);
        				 System.out.println("授课教师："+tea[cla[stu[check-1].c[i]].laoshi-1].name);
        				 System.out.println("时间："+cla[stu[check-1].c[i]].shijian);
        				 System.out.println("地点："+cla[stu[check-1].c[i]].didian);
                	 }
                 }
                 if (temp==5) {
            		 System.out.println("学生未选课");
            	 } else {System.out.println("以上为已选课程");}
                 System.out.println("----------------------------------------");
                 System.out.println("输入0选课，输入1退课，输入其它数字退出");  
                 celect=sc.nextInt();
                 if (celect==0) {
                	 while (true) {
        			 int check1,temp13=5 ; //创建一个输入
        			 System.out.println("----------------------------------------");
        			 System.out.println("现有课程（空表示无课程):");
        			 for (int i=0;i<kc;i++) {
        				 if (cla[i].laoshi>-1) {
        					 System.out.println((i+1)+".");
        					 Rtname(cla[i]);
        				 }
                     }
        			 System.out.println("----------------------------------------");
        			 System.out.println("输入需要选的课程编号来选择课程，输入其它数来返回上一级");
        			 check1 = sc.nextInt();
        			 if (check1<kc+1 && check1>0) {
        			 for (int i=0;i<5;i++) {
        				temp13 = temp13-1;
        				if (stu[check-1].c[i]==-1) {stu[check-1].c[i]=check1-1;System.out.println("选课成功");temp13 = temp13+1;break;}
        			 }
        			 if (temp13==0) {System.out.println("可选课程数不足,如要继续选课，可做退课处理");break;}
                	 }else{break;}}
                 }
                 else if (celect==1) {
                	 while (true) {
                	 int check2;
                	 System.out.println("----------------------------------------");
                	 System.out.println("已选课程（空表示无课程):");
                	 for (int i=0;i<5;i++) {
                		 if (stu[check-1].c[i]>-1) {
                         System.out.println((i+1)+".");
                         System.out.println ( cla[stu[check-1].c[i]].toString());
        				 Rtname(cla[stu[check-1].c[i]]);
                		 }
                     }
                	 System.out.println("----------------------------------------");
                	 System.out.println("选择想要退选的课程输入1-5,返回上一级输入其它数字");
                	 check2=sc.nextInt();
                	 if(check2<6 && check2>0) {
                	 stu[check-1].c[check2-1]=-1;System.out.println("退课成功");
                	 } else {break;}
                	 }
                 } else {break;}
        		 }
    }
    else if (check==2) {
    	System.out.println("----------------------------------------");
    	 System.out.println("新建老师输入0，现有老师输入编号");
 	    check = sc.nextInt();
 	    Boolean x;
 	    x=true;
 	    if (laoshirenshu<check) {x=false;System.out.println("输入错误");}
 	    if (check ==0) {
 	    	laoshirenshu ++;
 	        System.out.println("输入姓名");
 	        String name = sc.next();
 	        System.out.println("输入性别");
 	        String sex = sc.next();
 	        System.out.println("输入年龄");
 	        int  age = sc.nextInt();
 	        Teacher temp = new Teacher(name,laoshirenshu,sex,age);
             tea[laoshirenshu-1] = temp;   
            check=laoshirenshu;
 	    } 
 	    while (x) {
 	    int check3;
 	    System.out.println("----------------------------------------");
        Tt(tea[check-1]);
        if (tea[check-1].ct[0]>-1) {System.out.println("所授课程:"+cla[tea[check-1].ct[0]].mingcheng);}
        if (tea[check-1].ct[1]>-1) {System.out.println("所授课程:"+cla[tea[check-1].ct[1]].mingcheng);}
        System.out.println("----------------------------------------");
        if(tea[check-1].ct[0]>-1 && tea[check-1].ct[1]>-1) {break;}
        System.out.println("输入1添加课程，输入其它数返回上一级菜单");
        check3=sc.nextInt();
        if(check3==1) {
        	
        
        	if (tea[check-1].ct[1]>-1) {
        
        		System.out.println("----------------------------------------");
        	int laoshi = check;
        	System.out.println("输入时间");
 	        int shijian = sc.nextInt();
 	        System.out.println("输入地点");
	        int didian = sc.nextInt();
	        System.out.println("输入名称");
	        String mingcheng = sc.next();
        	    	Class temp = new Class(laoshi,shijian,didian,mingcheng);
        	    	kc++;
        	    	cla[kc-1]= temp;
        	    	tea[check-1].ct[0]=kc-1;
        	    	System.out.println("----------------------------------------");
        	}
        	else {
        		System.out.println("----------------------------------------");
        		int laoshi = check;
            	System.out.println("输入时间");
     	        int shijian = sc.nextInt();
     	        System.out.println("输入地点");
    	        int didian = sc.nextInt();
    	        System.out.println("输入名称");
    	        String mingcheng = sc.next();
            	    	Class temp = new Class(laoshi,shijian,didian,mingcheng);
            	    	kc++;
            	    	cla[kc-1]= temp;
            	    	
            	    	tea[check-1].ct[1]=kc-1;
            	    	System.out.println("----------------------------------------");
        	}
	
        }else {break;}
       
        
 	    }
    }
    else if (check==0) {d=false;
    
    ObjectOutputStream out2 = new ObjectOutputStream(new FileOutputStream(teaf));
    out2.writeObject(tea);
    out2.close();
	ObjectOutputStream out3 = new ObjectOutputStream(new FileOutputStream(claf));
    out3.writeObject(cla);
    out3.close();
	ObjectOutputStream out1 = new ObjectOutputStream(new FileOutputStream(stuf));
    out1.writeObject(stu);
    out1.close();  
    }
    else {System.out.println("输入错误");}
    }   
	}   
}
}
